import sys
import time

from Base.DataCollect import DataCollect
from Base.PraseExcel import PraseExcel
from Base.PublicFunction import render_tem, assemb_data


def run(filename,sheet,caseid_list):
    excel = PraseExcel(filename) #实例化一个对象
    excel.ReadExcel(sheet)

    if caseid_list == '0':
        for row in range(1,excel.rows):
            # 这里的row是int类型
            assemb_data(excel,filename,sheet,row)
            DataCollect.percent = str(round(DataCollect.success_num / DataCollect.execute_sum, 4) * 100) + '%'
            # start_time = time.time()
            # caseid = excel.get_caseid(row)
            # case_detail = {}
            # case_detail['caseid'] = int(caseid)
            # case_detail['desc'] = excel.get_casedesc(caseid)
            # case_detail['module'] = excel.get_casemoudle(caseid)
            # DataCollect.execute_sum += 1
            # try:
            #     excel.do_caseid(int(caseid) ) # 执行do_caseid（）中的函数
            #     # excel.kdt.quit()  #直接退出浏览器
            #     case_detail['res'] = '成功'
            #     case_detail['res_info'] = ""
            #     DataCollect.success_num += 1
            #
            # except Exception as e:
            #     case_detail['res'] = '失败'
            #     case_detail['res_info'] = e
            #     DataCollect.fail_sum += 1
            # excel.kdt.quit()#直接退出浏览器
            # case_detail['file_path'] = f'{filename}中{sheet}'
            # case_detail['run_time'] = str(round(time.time() - start_time,1)) + "s"
            # DataCollect.case_detail_list.append(case_detail)
            # DataCollect.percent = str(round(DataCollect.success_num / DataCollect.execute_sum,4)*100) + '%'

    else:
        caseid_list = caseid_list.split(',') #列表是一个字符串，需要切割
        for caseid in caseid_list:
            if caseid[0] == 'p':
                caseid_list = excel.get_caselist_byplan(caseid)  #先获取一个caseid_list的列表
                for caseid in caseid_list:
                    # 这里的caseid是浮点型
                    assemb_data(excel,filename, sheet, caseid)
                    # start_time = time.time()
                    # case_detail = {}
                    # case_detail['caseid'] = int(caseid)
                    # case_detail['desc'] = excel.get_casedesc(caseid)
                    # case_detail['module'] = excel.get_casemoudle(caseid)
                    # DataCollect.execute_sum  += 1
                    # try:
                    #     excel.do_caseid(int(caseid))
                    #     excel.kdt.quit()
                    #     case_detail['res'] = '成功'
                    #     case_detail['res_info'] = ""
                    #     DataCollect.success_num += 1
                    #
                    # except Exception as e:
                    #     case_detail['res'] = '失败'
                    #     case_detail['res_info'] = e
                    #     DataCollect.fail_sum += 1
                    #
                    # excel.kdt.quit()  # 直接退出浏览器
                    # case_detail['file_path'] = f'{filename}中{sheet}'
                    # case_detail['run_time'] = str(round(time.time() - start_time, 1)) + "s"
                    # DataCollect.case_detail_list.append(case_detail) #将case_detail字典加入到类DataCollect中的case_detail_list列表中

            else:
                #这里的caseid是字符串
                assemb_data(excel,filename, sheet, caseid)
                # start_time = time.time()
                # case_detail = {}
                # caseid = int(caseid)
                # case_detail['caseid'] =  caseid
                # case_detail['desc'] = excel.get_casedesc(caseid)
                # case_detail['module'] = excel.get_casemoudle(caseid)
                # DataCollect.execute_sum += 1
                # try:
                #     excel.do_caseid(int(caseid))
                #     # excel.kdt.quit()
                #     case_detail['res'] = '成功'
                #     case_detail['res_info'] = ""
                #     DataCollect.success_num += 1
                # except Exception as e:
                #     case_detail['res'] = '失败'
                #     case_detail['res_info'] = e
                #     DataCollect.fail_sum += 1
                #
                # excel.kdt.quit()  # 直接退出浏览器
                # case_detail['file_path'] = f'{filename}中{sheet}'
                # case_detail['run_time'] = str(round(time.time() - start_time, 1)) + "s"
                # DataCollect.case_detail_list.append(case_detail)

        DataCollect.percent = str(round(DataCollect.success_num / DataCollect.execute_sum, 4) * 100) + '%'

    DataCollect.time_stmp =time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))

    render_tem(DataCollect) #渲染模板传入的是类DataCollect



if __name__ == '__main__':
    print(sys.argv)
    # filename = '易订货系统测试用例1.xlsx '
    filename = sys.argv[1]
    sheet = sys.argv[2]
    # sheet ='Sheet1'
    caseid_list = sys.argv[3]
    # caseid_list = 'p3'
    run(filename,sheet,caseid_list)